package cn.edu.zafu.bigdata.x.yaojy;

public class ArrayElementAccess {
    public static int getElement(int[] arr, int index){
        if (index < 0 || index >= arr.length){
            throw new ArrayIndexOutOfBoundsException("索引超出数组范围！");
        }
        return arr[index];
    }

    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};

        int index = 5;
        try{
            int element = getElement(arr,index);
            System.out.println("数组元素:" + element);
        } catch (ArrayIndexOutOfBoundsException e){
            System.out.println("错误信息：" + e.getMessage());
        }

        index = 2;
        try{
            int element = getElement(arr,index);
            System.out.println("数组元素:" + element);
        } catch (ArrayIndexOutOfBoundsException e){
            System.out.println("错误信息：" + e.getMessage());
        }
    }
}
